package com.ocom.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityPersonnelPalmprint;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.human.dao.IDaoPersonnelPalmprint;
import com.ocom.human.service.PersonnelPalmprintService;
import com.ocom.mysql.aspect.DataSourceAspect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PersonnelPalmprintServiceImpl extends ServiceImpl<IDaoPersonnelPalmprint, EntityPersonnelPalmprint> implements PersonnelPalmprintService {

    @Autowired
    private IDaoPersonnelPalmprint dao;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    public boolean deletePalm(Long comId, Integer pId, String version) {
        QueryWrapper<EntityPersonnelPalmprint> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("p_id", pId);
        queryWrapper.eq("version", version);
        return remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    public boolean insertPalm(Long comId, EntityPersonnelPalmprint entityPersonnelPalmprint) {
        return saveOrUpdate(entityPersonnelPalmprint);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityPersonnelPalmprint> getByPIds(Long comId, List<Integer> pIds, String version) {
        QueryWrapper<EntityPersonnelPalmprint> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        if (StringUtils.isNotBlank(version)) {
            queryWrapper.eq("version", version);
        }
        queryWrapper.in("p_id", pIds);

        return this.list(queryWrapper);
    }
}
